package ie.com.stamply.security

class User {

	transient springSecurityService

	String username
	String password
        String email
        Date dateCreate
        Date dateLastLogin    
        Date dateLastUpdate
        boolean business
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

   

	static constraints = {
                username blank: false, unique: true            
		email blank: false, unique: true
		password blank: false
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role } as Set
	}

	def beforeInsert() {
	      encodePassword()
               
	}

	def beforeUpdate() {
              dateLastUpdate = new Date()
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
